<?php
/** Class Member
* Manager member
**/

class Member extends admin_controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->model('member/member_model');
	}

	public function index(){
		$this->breadcrumb = array(
			'Dashboard' => URL_ADMIN,
			'Member'	=> URL_ADMIN.'member',
		);

		$total_members = $this->member_model->getListMember(null, array('id' => 'desc'), null)->result_array();
		$this->template->title()
				->set(array('total_members' => $total_members))
			  	->build('admin/member/index');
	}

	public function add(){
		$this->breadcrumb = array(
			'Dashboard' => URL_ADMIN,
			'Member'	=> URL_ADMIN.'member',
			'Add a member' => ''
		);

		if($_POST) {

			if($this->member_model->checkEmail($this->input->post('email'))) {
				$this->session->set_flashdata('error', "This Email is already exists in database");
				redirect(URL_ADMIN.'member/add');
			} else {
				if(!empty($_FILES['avatar']['name']))
	            {
	                    $config['upload_path'] = './data_uploads/avatar/';
	                    $config['allowed_types'] = 'gif|jpg|png';
	                    $config['encrypt_name'] = TRUE;

	                    $this->load->library('upload', $config);

	                    if ( ! $this->upload->do_upload('avatar'))
	                    {
	                        echo $this->upload->display_errors();
	                        $image = null;
	                    }
	                    else
	                    {
	                        $up = $this->upload->data();
	                        $image = 'data_uploads/avatar/' . $up['file_name'];

	                        $imageWidth = $up['image_width'];
	                        if($imageWidth > 150)
	                        {
	                            $this->load->library('image_lib');
	                            $config['image_library'] = 'GD2';
	                            $config['source_image'] = $up['full_path'];
	                            $config['quality'] = 75;
	                            $config['maintain_ratio'] = TRUE;
	                            $config['master_dim'] = 'auto';
	                            $config['width'] = 150;
	                            $config['height'] = 150;

	                            $this->image_lib->initialize($config);
	                            $this->image_lib->resize();
	                        }

	                    }
	            } else {
	                $image = null;
	            }  
				
				$arr = array(
					'fullname'	=> $this->input->post('fullname'),
					'email'		=> 	$this->input->post('email'),
					'password'	=> md5(md5($this->input->post('password'))),
					'status'		=> 1,
					'avatar'	=> $image,
					'time_added' => time()
				);

				if($this->db->insert('member', $arr)) {
					$this->message('success', "New member has been added succussful");
					redirect(URL_ADMIN.'member');
				} else {
					$this->message('error', "Error");
					redirect(URL_ADMIN.'member');	
				}	
			}

			
		}

		$this->template->title()
			  	->build('admin/member/add');	
	}

	public function delete() {
		$id = (int)$this->uri->segment(4);
		if($this->member_model->delete($id)) {
			$info = array('status' => TRUE);
		} else {
			$info = array('status' => FALSE);
		}

		echo json_encode($info);
	}

}
/**End file member.php **/
/* File local: application/controller/admin/member.php */